package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Autowired
    AdChannelMapper adChannelMapper;
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //判断参数是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        //给page赋页数和每页条数
        Page page = new Page(dto.getPage(), dto.getSize());
        //创建lambda条件构造器
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        IPage result = page(page,lambdaQueryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //参数判空
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        //添加新增时间
        channel.setCreatedTime(new Date());
        //进行添加操作
        save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        updateById(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        AdChannel channel = getById(id);
        //判断频道是否存在
        if (channel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断频道状态是否有效
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }

        //另一种异常处理信息
        //判断删除操作是否成功
//        boolean b = removeById(id);
//        if (b){
//            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//        }else {
//            return ResponseResult.okResult(AppHttpCodeEnum.SHI_BAI);
//        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

}
